package com.wx.plugin.service;

import com.wx.plugin.constant.ValueConstant;
import com.wx.plugin.repository.Members_accountRepository;
import com.wx.plugin.repository.Members_account_logRepository;
import com.yangzcp.userpojo.Members;
import com.yangzcp.userpojo.Members_account;
import com.yangzcp.userpojo.Members_account_log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class AcctransService {
    @Autowired
    private Members_accountRepository members_accountRepository;
    @Autowired
    private Members_account_logRepository members_account_logRepository;


    public void jianAccount(Members members, BigDecimal amount, String orderNo, Integer reson_type, String reson){
        Members_account account = members_accountRepository.findByMemberId(members.getId());
        account.setMoney(account.getMoney().subtract(amount));
        account.setGain_money(account.getGain_money().subtract(amount));
        account.setVersion(account.getVersion() + 1);
        account.setUpdate_time(new Date().getTime());
        members_accountRepository.save(account);
        Members_account_log account_log = new Members_account_log();
        account_log.setAmount(amount.multiply(ValueConstant.DUI_HUAN_FU));
        account_log.setOrder_no(orderNo);
        account_log.setRef_member(members);
        account_log.setReson_type(reson_type);
        account_log.setReson(reson);
        account_log.setSale_flag(members.getSale_flag());
        account_log.setSale_level(members.getSale_level());
        members_account_logRepository.save(account_log);
    }
}
